package com.oracle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oracle.mapper.EquipmentMapper;
import com.oracle.pojo.Equipment;
import com.oracle.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class EquipmentServiceImpl implements EquipmentService {


    @Autowired
    private EquipmentMapper equipmentMapper;

    @Override
    @Transactional(readOnly = true ,propagation = Propagation.SUPPORTS)
    public IPage<Equipment> findEquipmentPage(Integer pageNum, Integer pageSize, QueryWrapper<Equipment> queryWrapper) {
        IPage<Equipment> page = new Page<>(pageNum, pageSize, true);
        IPage<Equipment> equipmentIPage = equipmentMapper.selectPage(page, queryWrapper);
        return equipmentIPage;
    }

    @Override
    @Transactional(readOnly = true ,propagation = Propagation.SUPPORTS)
    public List<Equipment> findAllEquipment() {
        List<Equipment> equipment = equipmentMapper.selectList(null);
        return equipment;
    }

    @Override
    @Transactional(readOnly = true ,propagation = Propagation.SUPPORTS)
    public Equipment findOneEquipment(Integer id) {
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("equipment_Id", id);
        Equipment equipment = equipmentMapper.selectOne(queryWrapper);
        return equipment;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int insertEquipment(Equipment equipment) {
        int insert = equipmentMapper.insert(equipment);
        return insert;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int updataEquipment(Equipment equipment) {
        Integer equipmentId = equipment.getEquipmentId();
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("equipment_Id", equipmentId);
        int i = equipmentMapper.update(equipment,queryWrapper);
        return i;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int updataState(Equipment equipment) {
        UpdateWrapper<Equipment> updateWrapper = new UpdateWrapper<>();
        Integer equipmentId = equipment.getEquipmentId();
        updateWrapper.eq("id", equipmentId);
        updateWrapper.set("state", 1);
        int update = equipmentMapper.update(null, updateWrapper);
        return update;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public int deleteEquipment(Integer equipmentId) {
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("equipment_Id", equipmentId);
        int delete = equipmentMapper.delete(queryWrapper);
        return delete;
    }


}
